home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / wu_ftpd_37_21.lha / wu-ftpd / src / conversions.c < prev    next >
C/C++ Source or Header  |  1994-07-24  |  6KB  |  200 lines

  1. /* Copyright (c) 1993, 1994  Washington University in Saint Louis
  2.  * All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are
  6.  * met: 1. Redistributions of source code must retain the above copyright
  7.  * notice, this list of conditions and the following disclaimer. 2.
  8.  * Redistributions in binary form must reproduce the above copyright notice,
  9.  * this list of conditions and the following disclaimer in the documentation
  10.  * and/or other materials provided with the distribution. 3. All advertising
  11.  * materials mentioning features or use of this software must display the
  12.  * following acknowledgement: This product includes software developed by the
  13.  * Washington University in Saint Louis and its contributors. 4. Neither the
  14.  * name of the University nor the names of its contributors may be used to
  15.  * endorse or promote products derived from this software without specific
  16.  * prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY WASHINGTON UNIVERSITY AND CONTRIBUTORS
  19.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASHINGTON
  22.  * UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. #include "config.h"
  33.  
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #ifdef SYSSYSLOG
  37. #include <sys/syslog.h>
  38. #else
  39. #include <syslog.h>
  40. #endif
  41. #include <string.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include "conversions.h"
  45. #include "extensions.h"
  46. #include "pathnames.h"
  47.  
  48. /*************************************************************************/
  49. /* FUNCTION  : readconv                                                  */
  50. /* PURPOSE   : Read the conversions into memory                          */
  51. /* ARGUMENTS : The pathname of the conversion file                       */
  52. /* RETURNS   : 0 if error, 1 if no error                                 */
  53. /*************************************************************************/
  54.  
  55. char *convbuf = NULL;
  56. struct convert *cvtptr;
  57.  
  58. struct str2int {
  59.     char *string;
  60.     int value;
  61. };
  62.  
  63. struct str2int c_list[] =
  64. {"T_REG", T_REG,
  65.  "T_ASCII", T_ASCII,
  66.  "T_DIR", T_DIR,
  67.  "O_COMPRESS", O_COMPRESS,
  68.  "O_UNCOMPRESS", O_UNCOMPRESS,
  69.  "O_TAR", O_TAR,
  70.  NULL, 0,};
  71.  
  72. int
  73. conv(char *str)
  74. {
  75.     int rc = 0;
  76.     int counter;
  77.  
  78.     /* check for presence of ALL items in string... */
  79.  
  80.     for (counter = 0; c_list[counter].string; ++counter)
  81.         if (strstr(str, c_list[counter].string))
  82.             rc = rc | c_list[counter].value;
  83.     return (rc);
  84. }
  85.  
  86. int
  87. readconv(char *convpath)
  88. {
  89.     FILE *convfile;
  90.     struct stat finfo;
  91.  
  92.     if (stat(convpath, &finfo) != 0) {
  93.         syslog(LOG_ERR, "cannot stat conversion file %s: %s", convpath,
  94.                strerror(errno));
  95.         return (0);
  96.     }
  97.     if ((convfile = fopen(convpath, "r")) == NULL) {
  98.         if (errno != ENOENT)
  99.             syslog(LOG_ERR, "cannot open conversion file %s: %s",
  100.                    convpath, strerror(errno));
  101.         return (0);
  102.     }
  103.     if (finfo.st_size == 0) {
  104.         convbuf = (char *) calloc(1, 1);
  105.     } else {
  106.         if (!(convbuf = (char *) malloc((unsigned) finfo.st_size + 1))) {
  107.             syslog(LOG_ERR, "could not malloc convbuf (%d bytes)", finfo.st_size + 1);
  108.             return (0);
  109.         }
  110.         if (!fread(convbuf, (size_t) finfo.st_size, 1, convfile)) {
  111.             syslog(LOG_ERR, "error reading conv file %s: %s", convpath,
  112.                    strerror(errno));
  113.             convbuf = NULL;
  114.             return (0);
  115.         }
  116.         *(convbuf + finfo.st_size) = '\0';
  117.     }
  118.     return (1);
  119. }
  120.  
  121. void
  122. parseconv(void)
  123. {
  124.     char *ptr;
  125.     char *convptr = convbuf,
  126.      *line;
  127.     char **argv[51],
  128.     **ap = (char **) argv,
  129.      *p,
  130.      *val;
  131.     struct convert *cptr;
  132.  
  133.     if (!convbuf || !(*convbuf))
  134.         return;
  135.  
  136.     /* build list, initialize to zero. */
  137.     cvtptr = (struct convert *) calloc(1, sizeof(struct convert));
  138.  
  139.     /* read through convbuf, stripping comments. */
  140.     while (*convptr != '\0') {
  141.         line = convptr;
  142.         while (*convptr && *convptr != '\n')
  143.             convptr++;
  144.         *convptr++ = '\0';
  145.  
  146.         /* deal with comments */
  147.         if ((ptr = strchr(line, '#')) != NULL)
  148.             *ptr = '\0';
  149.  
  150.         if (*line == '\0')
  151.             continue;
  152.  
  153.         ap = (char **) argv;
  154.  
  155.         /* parse the lines... */
  156.         for (p = line; p != NULL;) {
  157. #ifndef AMIGA
  158.             while ((val = (char *) strsep(&p, ":\n")) != NULL && *val == '\0') ;
  159. #else
  160.             while ((val = (char *) strsep(&p, ";\n")) != NULL && *val == '\0') ;
  161. #endif
  162.             *ap = val;
  163.             if (**ap == ' ')
  164.                 *ap = NULL;
  165.             *ap++;
  166.         }
  167.         *ap = 0;
  168.  
  169.         /* if we didn't read 7 things, skip that line... */
  170.  
  171.         /* add element to beginning of list */
  172.         cptr = (struct convert *) calloc(1, sizeof(struct convert));
  173.  
  174.         cptr->next = cvtptr;
  175.         cvtptr = cptr;
  176.  
  177.         cptr->stripprefix = (char *) argv[0];
  178.         cptr->stripfix = (char *) argv[1];
  179.         cptr->prefix = (char *) argv[2];
  180.         cptr->postfix = (char *) argv[3];
  181.         cptr->external_cmd = (char *) argv[4];
  182.         cptr->types = conv((char *) argv[5]);
  183.         cptr->options = conv((char *) argv[6]);
  184.         cptr->name = (char *) argv[7];
  185.     }
  186. }
  187.  
  188. void
  189. conv_init(void)
  190. {
  191. #ifdef VERBOSE
  192.     struct convert *cptr;
  193.  
  194. #endif
  195.  
  196.     if ((readconv(_PATH_CVT)) < 0)
  197.         return;
  198.     parseconv();
  199. }
  200.